
type VideoLoadOpts = {
    autoPlay?: boolean
    loop?: boolean
    autoSize?: boolean
    onPlay?(video: Video): void;
    afterRender?(video: Video): void;
    onLoadCompleted?(video: Video): void;
}
class Video extends eui.Image {
    constructor() {
        super()
        this.width = 256
        this.height = 256
    }
    childrenCreated() {
        super.childrenCreated()
    }
    public static get IsWasmSupported(): boolean {
        return JSMpeg["WASMModule"].IsSupported()
    }
    public static get RenderContext(): any {
        return egret["web"].WebGLRenderContext.getInstance()
    }
    public static get GL(): WebGLRenderingContext {
        return egret["web"].WebGLRenderContext.getInstance().context
    }

    private _videoTexture: WebGLTexture = null
    private _bitmapData: egret.BitmapData = null
    private _player: JSMpeg.Player = null

    public get VideoTexture(): WebGLTexture {
        return this._videoTexture
    }
    public get Texture(): egret.Texture {
        let bmp = new egret.BitmapData(null);
        bmp.webGLTexture = this._videoTexture
        bmp.width = this.width
        bmp.height = this.height
        let tex = new egret.Texture()
        tex._setBitmapData(bmp)
        return tex
    }

    public Load(source: string, opts: VideoLoadOpts) {
        let url = source
        let res = RES.getResourceInfo(source)
        if (res) {
            url = res.root + res.url
        }
        let gl = Video.GL
        let img = new ImageData(this.width, this.height)
        let data: any = img
        //this._videoTexture = egret.sys.createTexture(Video.RenderContext, data)
        if (this._videoTexture == null) {
            this._videoTexture = gl.createTexture();
            this._videoTexture[egret.glContext] = gl;
            gl.bindTexture(gl.TEXTURE_2D, this._videoTexture);
            gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
            //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmapData);
            gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null)
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
            gl.bindTexture(gl.TEXTURE_2D, null);
        }
        this._player = new JSMpeg.Player(url, {
            gl: gl,
            width: this.width,
            height: this.height,
            renderTexture: this._videoTexture,
            loop: opts.loop, autoplay: opts.autoPlay, progressive: false,
            onPlay: () => {
                this.onPlay()
                if (opts.onPlay) {
                    opts.onPlay(this)
                }
            },
            afterRender: () => {
                Video.resetGL()
                if (opts.afterRender) {
                    opts.afterRender(this)
                }
            },
            onSourceCompleted: () => {
                if (opts.onLoadCompleted) {
                    opts.onLoadCompleted(this)
                }
            },
            onVideoDecode: () => {
            }
        })
        Video.resetGL()
    }

    private onPlay() {
        // let tex = new egret.Texture()
        // tex.bitmapData = new egret.BitmapData(tex);
        // tex.$bitmapWidth = tex.bitmapData.width = this.width
        // tex.$bitmapHeight = tex.bitmapData.height = this.height
        // tex.bitmapData.webGLTexture = this._videoTexture
        this.source = this.Texture
    }

    private static resetGL() {
        Video.RenderContext.currentProgram = null
        Video.RenderContext.onResize()
        Video.GL.bindBuffer(Video.GL.ARRAY_BUFFER, Video.RenderContext.vertexBuffer)
    }


}